<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="120px"
             class="demo-form-inline">
      <el-form-item label="方案id" prop="planId">
        <el-select v-model="queryParams.planId" placeholder="请选择方案id" clearable>
          <el-option
            v-for="item in planList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报告标题" prop="reportTitle">
        <el-input
          v-model="queryParams.reportTitle"
          placeholder="请输入报告标题"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-perms="['judicial:identification_report:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="identification_reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="鉴定方案" align="center" prop="planId">
        <template #default="scope">
          {{ planList.find(e => e.id == scope.row.planId)?.title }}
        </template>
      </el-table-column>
      <el-table-column label="报告标题" align="center" prop="reportTitle" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <status-tag :status-enum="AuditStatus" :status="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-perms="['judicial:identification_report:edit']"
          >修改
          </el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-perms="['judicial:identification_report:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total>0"
      v-model:current-page="queryParams.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      style="float: right"
      @change="(currentPage, pageSize) => {this.queryParams.pageNum = currentPage;this.getList()}"
    />
    <!-- 添加或修改鉴定报告对话框 -->
    <el-dialog :title="title" v-model="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="方案id" prop="planId">
          <el-select v-model="form.planId" @change="selectChange" placeholder="请选择方案id">
            <el-option
              v-for="item in planList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报告标题" prop="reportTitle">
          <el-input v-model="form.reportTitle" placeholder="请输入报告标题" />
        </el-form-item>
        <el-form-item label="报告内容">
          <my-editor :content="form.reportContent" @input="handleEditorContentChange" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="item in convertToOptions(AuditStatus)" :key="item.value" size="large"
                      :value="item.value.toString()">{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listIdentification_report,
  getIdentification_report,
  delIdentification_report,
  addIdentification_report,
  updateIdentification_report
} from '@/api/judicial/identification_report'
import { listIdentification_plan } from '@/api/judicial/identification_plan.js'
import MyEditor from '@/components/MyEditor/index.vue'
import _ from 'lodash'
import StatusTag from '@/components/StatusTag.vue'
import { AuditStatus } from '@/enum.js'
import { convertToOptions } from '@/utils/util.js'

export default {
  name: 'Identification_report',
  computed: {
    AuditStatus() {
      return AuditStatus
    }
  },
  components: { StatusTag, MyEditor },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 鉴定报告表格数据
      identification_reportList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planId: null,
        reportTitle: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        planId: [
          { required: true, message: '方案id不能为空', trigger: 'change' }
        ],
        reportTitle: [
          { required: true, message: '报告标题不能为空', trigger: 'blur' }
        ],
        reportContent: [
          { required: true, message: '报告内容不能为空', trigger: 'blur' }
        ]
      },
      planList: []
    }
  },
  created() {
    this.getList()
    this.getPlanList()
  },
  methods: {
    convertToOptions,
    getPlanList() {
      listIdentification_plan().then(resp => {
        this.planList = resp.rows
      })
    },
    /** 查询鉴定报告列表 */
    getList() {
      if (this.$route.query.id) {
        this.queryParams.caseId = this.$route.query.id
      }
      this.loading = true
      listIdentification_report(this.queryParams).then(response => {
        this.identification_reportList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        planId: null,
        reportTitle: null,
        reportContent: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加鉴定报告'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getIdentification_report(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改鉴定报告'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateIdentification_report(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addIdentification_report(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除鉴定报告编号为"' + ids + '"的数据项？').then(function() {
        return delIdentification_report(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    },
    handleEditorContentChange(e) {
      this.form.reportContent = e
    },
    selectChange(e) {
      let caseInfo = _.find(this.planList, { 'id': e })
      if (caseInfo)
        this.form.caseId = caseInfo.caseId
    }
  }
}
</script>
